package com.youlai.boot.modules.webapp.controller;

import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.webapp.service.IdPhotoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "证件照接口")
@RestController
@RequestMapping("/api/webapp")
@RequiredArgsConstructor
public class IdPhotoController {

    private final IdPhotoService idPhotoService;

    @Operation(summary = "证件照生成")
    @PostMapping("/idphoto")
    public Result<Map<String, Object>> generateIdPhoto(@RequestParam("input_image") MultipartFile image,
                                          @RequestParam(value = "spec", required = false) String spec) {
        Map<String, Object> map = idPhotoService.generateIdPhoto(image, spec);
        return Result.success(map);
    }

    @Operation(summary = "添加背景")
    @PostMapping("/add_background")
    public Result<Map<String, Object>> addBackground(@RequestParam("input_image") MultipartFile image,
                                        @RequestParam("color") String color) {
        Map<String, Object> photoUrl = idPhotoService.addBackground(image, color);
        return Result.success(photoUrl);
    }

    @Operation(summary = "人像抠图")
    @PostMapping("/human_matting")
    public Result<String> humanMatting(@RequestParam("input_image") MultipartFile image) {
        String photoUrl = idPhotoService.humanMatting(image);
        return Result.success(photoUrl);
    }

    @Operation(summary = "设置证件照规格")
    @PostMapping("/set_kb")
    public Result<Void> setPhotoSpec(@RequestBody Map<String, Object> spec) {
        idPhotoService.setPhotoSpec(spec);
        return Result.success();
    }

    @Operation(summary = "生成排版照片")
    @PostMapping("/generate_layout_photos")
    public Result<String> generateLayoutPhotos(@RequestParam("input_image") MultipartFile image,
                                              @RequestParam("layout") String layout) {
        String photoUrl = idPhotoService.generateLayoutPhotos(image, layout);
        return Result.success(photoUrl);
    }

    @Operation(summary = "添加水印")
    @PostMapping("/watermark")
    public Result<String> addWatermark(@RequestParam("input_image") MultipartFile image,
                                       @RequestParam("text") String text) {
        String photoUrl = idPhotoService.addWatermark(image, text);
        return Result.success(photoUrl);
    }

    @Operation(summary = "证件照裁剪")
    @PostMapping("/crop")
    public Result<String> idPhotoCrop(@RequestParam("input_image") MultipartFile image,
                                     @RequestParam("x") Integer x,
                                     @RequestParam("y") Integer y,
                                     @RequestParam("width") Integer width,
                                     @RequestParam("height") Integer height) {
        String photoUrl = idPhotoService.idPhotoCrop(image, x, y, width, height);
        return Result.success(photoUrl);
    }
} 